Move `resolve_dependencies` into resolve.rs
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Dec 2016 08:01:01 +0000 (11:01 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 16 Dec 2016 08:01:01 +0000 (11:01 +0300)
src/cargo/ops/cargo_compile.rs
src/cargo/ops/mod.rs
src/cargo/ops/resolve.rs

index 1102f4b7f9fdb443979a5aa7724431c8d4ba25f2..b2027b9917bc74f9cf294626eab44a2e78b07457 100644 (file)
 use std::collections::HashMap;
 use std::path::PathBuf;
 
-use core::registry::PackageRegistry;
-use core::{Source, SourceId, PackageSet, Package, Target};
+use core::{Source, Package, Target};
 use core::{Profile, TargetKind, Profiles, Workspace, PackageIdSpec};
-use core::resolver::{Method, Resolve};
 use ops::{self, BuildOutput};
-use sources::PathSource;
 use util::config::Config;
-use util::{CargoResult, profile, human, ChainError};
+use util::{CargoResult, profile};
 
 /// Contains information about how a package should be compiled.
 pub struct CompileOptions<'a> {
@@ -124,65 +121,6 @@ pub fn compile<'a>(ws: &Workspace<'a>, options: &CompileOptions<'a>)
     compile_ws(ws, None, options)
 }
 
-pub fn resolve_dependencies<'a>(ws: &Workspace<'a>,
-                                source: Option<Box<Source + 'a>>,
-                                features: &[String],
-                                all_features: bool,
-                                no_default_features: bool,
-                                specs: &[PackageIdSpec])
-                                -> CargoResult<(PackageSet<'a>, Resolve)> {
-    let features = features.iter().flat_map(|s| {
-        s.split_whitespace()
-    }).map(|s| s.to_string()).collect::<Vec<String>>();
-
-    let mut registry = PackageRegistry::new(ws.config())?;
-
-    if let Some(source) = source {
-        if let Some(root_package) = ws.current_opt() {
-            registry.add_preloaded(root_package.package_id().source_id(), source);
-        }
-    }
-
-    // First, resolve the root_package's *listed* dependencies, as well as
-    // downloading and updating all remotes and such.
-    let resolve = ops::resolve_ws(&mut registry, ws)?;
-
-    // Second, resolve with precisely what we're doing. Filter out
-    // transitive dependencies if necessary, specify features, handle
-    // overrides, etc.
-    let _p = profile::start("resolving w/ overrides...");
-
-    add_overrides(&mut registry, ws)?;
-
-    let method = if all_features {
-        Method::Everything
-    } else {
-        Method::Required {
-            dev_deps: true, // TODO: remove this option?
-            features: &features,
-            uses_default_features: !no_default_features,
-        }
-    };
-
-    let resolved_with_overrides =
-            ops::resolve_with_previous(&mut registry, ws,
-                                            method, Some(&resolve), None,
-                                            &specs)?;
-
-    for &(ref replace_spec, _) in ws.root_replace() {
-        if !resolved_with_overrides.replacements().keys().any(|r| replace_spec.matches(r)) {
-            ws.config().shell().warn(
-                format!("package replacement is not used: {}", replace_spec)
-            )?
-        }
-    }
-
-    let packages = ops::get_resolved_packages(&resolved_with_overrides,
-                                              registry);
-
-    Ok((packages, resolved_with_overrides))
-}
-
 pub fn compile_ws<'a>(ws: &Workspace<'a>,
                       source: Option<Box<Source + 'a>>,
                       options: &CompileOptions<'a>)
@@ -203,12 +141,12 @@ pub fn compile_ws<'a>(ws: &Workspace<'a>,
     let profiles = ws.profiles();
 
     let specs = spec.into_package_id_specs(ws)?;
-    let resolve = resolve_dependencies(ws,
-                                       source,
-                                       features,
-                                       all_features,
-                                       no_default_features,
-                                       &specs)?;
+    let resolve = ops::resolve_dependencies(ws,
+                                            source,
+                                            features,
+                                            all_features,
+                                            no_default_features,
+                                            &specs)?;
     let (packages, resolve_with_overrides) = resolve;
 
     let mut pkgids = Vec::new();
@@ -439,35 +377,6 @@ fn generate_targets<'a>(pkg: &'a Package,
     }
 }
 
-/// Read the `paths` configuration variable to discover all path overrides that
-/// have been configured.
-fn add_overrides<'a>(registry: &mut PackageRegistry<'a>,
-                     ws: &Workspace<'a>) -> CargoResult<()> {
-    let paths = match ws.config().get_list("paths")? {
-        Some(list) => list,
-        None => return Ok(())
-    };
-
-    let paths = paths.val.iter().map(|&(ref s, ref p)| {
-        // The path listed next to the string is the config file in which the
-        // key was located, so we want to pop off the `.cargo/config` component
-        // to get the directory containing the `.cargo` folder.
-        (p.parent().unwrap().parent().unwrap().join(s), p)
-    });
-
-    for (path, definition) in paths {
-        let id = SourceId::for_path(&path)?;
-        let mut source = PathSource::new_recursive(&path, &id, ws.config());
-        source.update().chain_error(|| {
-            human(format!("failed to update path override `{}` \
-                           (defined in `{}`)", path.display(),
-                          definition.display()))
-        })?;
-        registry.add_override(&id, Box::new(source));
-    }
-    Ok(())
-}
-
 /// Parse all config files to learn about build configuration. Currently
 /// configured options are:
 ///
index 2b46cc4d26a861acf31f52aafc0a4daaf4fbb3a8..ce6630cfdd36036be3995cbb589322a75bdbf4d0 100644 (file)
@@ -1,5 +1,5 @@
 pub use self::cargo_clean::{clean, CleanOptions};
-pub use self::cargo_compile::{compile, compile_ws, resolve_dependencies, CompileOptions};
+pub use self::cargo_compile::{compile, compile_ws, CompileOptions};
 pub use self::cargo_compile::{CompileFilter, CompileMode, MessageFormat, Packages};
 pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
 pub use self::cargo_rustc::{compile_targets, Compilation, Kind, Unit};
@@ -20,7 +20,7 @@ pub use self::registry::{registry_login, search, http_proxy_exists, http_handle}
 pub use self::registry::{modify_owners, yank, OwnersOptions, PublishOpts};
 pub use self::cargo_fetch::{fetch, get_resolved_packages};
 pub use self::cargo_pkgid::pkgid;
-pub use self::resolve::{resolve_ws, resolve_with_previous};
+pub use self::resolve::{resolve_ws, resolve_dependencies, resolve_with_previous};
 pub use self::cargo_output_metadata::{output_metadata, OutputMetadataOptions, ExportInfo};
 
 mod cargo_clean;
index 335c06b88d784c4604f063511f3b5c50eb63185d..364335a77618e0a57353fea3f2face992173aeaa 100644 (file)
@@ -1,10 +1,11 @@
 use std::collections::HashSet;
 
-use core::{PackageId, PackageIdSpec, SourceId, Workspace};
+use core::{PackageId, PackageIdSpec, PackageSet, Source, SourceId, Workspace};
 use core::registry::PackageRegistry;
 use core::resolver::{self, Resolve, Method};
+use sources::PathSource;
+use util::{profile, human, CargoResult, ChainError};
 use ops;
-use util::CargoResult;
 
 /// Resolve all dependencies for the specified `package` using the previous
 /// lockfile as a guide if present.
@@ -25,6 +26,65 @@ pub fn resolve_ws(registry: &mut PackageRegistry, ws: &Workspace)
     Ok(resolve)
 }
 
+pub fn resolve_dependencies<'a>(ws: &Workspace<'a>,
+                                source: Option<Box<Source + 'a>>,
+                                features: &[String],
+                                all_features: bool,
+                                no_default_features: bool,
+                                specs: &[PackageIdSpec])
+                                -> CargoResult<(PackageSet<'a>, Resolve)> {
+    let features = features.iter().flat_map(|s| {
+        s.split_whitespace()
+    }).map(|s| s.to_string()).collect::<Vec<String>>();
+
+    let mut registry = PackageRegistry::new(ws.config())?;
+
+    if let Some(source) = source {
+        if let Some(root_package) = ws.current_opt() {
+            registry.add_preloaded(root_package.package_id().source_id(), source);
+        }
+    }
+
+    // First, resolve the root_package's *listed* dependencies, as well as
+    // downloading and updating all remotes and such.
+    let resolve = resolve_ws(&mut registry, ws)?;
+
+    // Second, resolve with precisely what we're doing. Filter out
+    // transitive dependencies if necessary, specify features, handle
+    // overrides, etc.
+    let _p = profile::start("resolving w/ overrides...");
+
+    add_overrides(&mut registry, ws)?;
+
+    let method = if all_features {
+        Method::Everything
+    } else {
+        Method::Required {
+            dev_deps: true, // TODO: remove this option?
+            features: &features,
+            uses_default_features: !no_default_features,
+        }
+    };
+
+    let resolved_with_overrides =
+    ops::resolve_with_previous(&mut registry, ws,
+                               method, Some(&resolve), None,
+                               &specs)?;
+
+    for &(ref replace_spec, _) in ws.root_replace() {
+        if !resolved_with_overrides.replacements().keys().any(|r| replace_spec.matches(r)) {
+            ws.config().shell().warn(
+                format!("package replacement is not used: {}", replace_spec)
+            )?
+        }
+    }
+
+    let packages = ops::get_resolved_packages(&resolved_with_overrides,
+                                              registry);
+
+    Ok((packages, resolved_with_overrides))
+}
+
 /// Resolve all dependencies for a package using an optional previous instance
 /// of resolve to guide the resolution process.
 ///
@@ -169,3 +229,32 @@ pub fn resolve_with_previous<'a>(registry: &mut PackageRegistry,
         }
     }
 }
+
+/// Read the `paths` configuration variable to discover all path overrides that
+/// have been configured.
+fn add_overrides<'a>(registry: &mut PackageRegistry<'a>,
+                     ws: &Workspace<'a>) -> CargoResult<()> {
+    let paths = match ws.config().get_list("paths")? {
+        Some(list) => list,
+        None => return Ok(())
+    };
+
+    let paths = paths.val.iter().map(|&(ref s, ref p)| {
+        // The path listed next to the string is the config file in which the
+        // key was located, so we want to pop off the `.cargo/config` component
+        // to get the directory containing the `.cargo` folder.
+        (p.parent().unwrap().parent().unwrap().join(s), p)
+    });
+
+    for (path, definition) in paths {
+        let id = SourceId::for_path(&path)?;
+        let mut source = PathSource::new_recursive(&path, &id, ws.config());
+        source.update().chain_error(|| {
+            human(format!("failed to update path override `{}` \
+                           (defined in `{}`)", path.display(),
+                          definition.display()))
+        })?;
+        registry.add_override(&id, Box::new(source));
+    }
+    Ok(())
+}